package com.example.tvidemo.controller;

import com.example.tvidemo.entity.Permission;
import com.example.tvidemo.rest.ResponseData;
import com.example.tvidemo.service.PermissionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理控制器
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value="获取所有权限详细信息", notes="获取所有权限详细信息")
    @GetMapping("/noaccess/findAll")
    public ResponseData findAll(){
        List<Permission> permissions = permissionService.findAll();
        return new ResponseData(200,"请求成功",permissions);
    }

    @ApiOperation(value="获取权限详细信息", notes="根据url的id来获取权限详细信息")
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "String",paramType="path")
    @GetMapping("/get/{id}")
    public ResponseData getUser(@PathVariable("id") String id){
        Permission permission = permissionService.findById(id);
        return new ResponseData(200,"请求成功",permission);
    }

    @ApiOperation(value="创建权限", notes="根据Permission对象创建权限")
    @ApiImplicitParam(name = "permission", value = "权限详细实体permission", required = true, dataType = "Permission")
    @PostMapping("/add")
    public ResponseData addUser(@RequestBody Permission permission){
        permissionService.addPermission(permission);
        return new ResponseData(200,"添加成功",permission);
    }

    @ApiOperation(value="删除权限", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "String",paramType="path")
    @DeleteMapping("/delete/{id}")
    public ResponseData delUser(@PathVariable("id") String id){
        permissionService.deletePermission(id);
        return new ResponseData(200,"删除成功",null);
    }

    @ApiOperation(value="更新权限详细信息", notes="根据url的id来指定更新对象，并根据传过来的permission信息来更新权限详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "String",paramType="path"),
            @ApiImplicitParam(name = "permission", value = "用户详细实体permission", required = true, dataType = "Permission")
    })
    @PutMapping("/update/{id}")
    public ResponseData updateUser(@PathVariable("id") String id,@RequestBody Permission permission){
        Permission updatePermission = permissionService.findById(id);
        updatePermission.setPermissionName(permission.getPermissionName());
        updatePermission.setPermissionEnname(permission.getPermissionName());
        updatePermission.setDescription(permission.getDescription());
        updatePermission.setUrl(permission.getUrl());
        updatePermission.setMethod(updatePermission.getMethod());
        return new ResponseData(200,"修改成功",null);
    }
}
